﻿namespace GoFun.Lib

open Positionen

type Kette = Position Set

module Ketten =
    
    let ketteVon (brett : Brett) (position : Position) : Kette =
        let dim = brettDim brett
        let zielStatus = positionStatus position brett
        let alreadyLookedAt = new System.Collections.Generic.HashSet<_>()
        let lookedAt (pos : Position) = alreadyLookedAt.Add pos |> ignore
        let hasLookedAt (pos : Position) = alreadyLookedAt.Contains pos
        let rec findMore posToLookAt found =
            match posToLookAt with
            | [] -> found
            | pos::rest ->
                lookedAt pos
                if positionStatus pos brett <> zielStatus
                then findMore rest found
                else
                    let weitersuchen =
                        surrounding dim pos
                        |> List.filter (not << hasLookedAt)
                    findMore (weitersuchen@rest) (pos::found)
        Set.ofList (findMore [position] [])
                    